﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SQLServerHealthCheck.Model;


namespace SQLServerHealthCheck.SQLServer.IssueTypes
{
    public class ServiceCheckOleAutomationEnabled:IssueTypeCheckBase
    {

        public ServiceCheckOleAutomationEnabled()
        {
            IssueTypeId = 2005;
        }

        public override void Check()
        {
            // Service check Ole Automation is enables
            HealthCheckDataContext dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"]);


            /*
             * This rule checks if OLE procedures is enabled 
             */

            var Qry = from ss in dc.ServiceSettings

                      where (ss.Name == "Ole Automation Procedures") && (Convert.ToInt32(ss.Value) == 1)


                      select ss;  

            foreach (ServiceSetting serviceSetting in Qry)
            {
                IssueType issueType = dc.IssueTypes.FirstOrDefault(i => i.IssueTypeId == IssueTypeId);

                //Create new Issue

                Issue issue = new Issue();
                issue.IssueType = issueType;
                issue.TargetId = serviceSetting.ServiceId;

                string serviceName = serviceSetting.Service.InstanceName;
               
                issue.Description = issueType.Description.Replace("{InstanceName}", serviceSetting.Service.InstanceName);

                dc.Issues.InsertOnSubmit(issue);
            }

            dc.SubmitChanges();
        
        }
    
    }
}
